﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CL_SQL.DAL
{
    public class typeDAL
    {

        LinqDBDataContext lqDB= new LinqDBDataContext("Data Source=.;Initial Catalog=StudentLoan;Persist Security Info=True;User ID=sa;Password=sa");
        internal List<Type> Alltype()
        { 
            var result = from d in lqDB.Type select d;
            return result.ToList<Type>();
        }

        internal Boolean Edit(Type type)
        {
            try
            {
                var target = (from g in lqDB.Type where g.TypeId == type.TypeId select g).ToList()[0];
                target.TypeId = type.TypeId;
                target.Typename = type.Typename;
                target.Sum = type.Sum;
                target.LastModifyDate = type.LastModifyDate;
                target.LastModifyUser = type.LastModifyUser;
                lqDB.SubmitChanges();
            }
            catch (Exception ex)
            {
                return false;
                throw (ex);
            }
            return true;
        }
        internal Boolean Add(Type type)
        {
            try
            {
                var temp = from g in lqDB.Type select g.TypeId;
                if (!(temp.Count() > 0))
                    type.TypeId = 0;
                else
                    type.TypeId = temp.Max() + 1;
                lqDB.Type.InsertOnSubmit(type);
                lqDB.SubmitChanges();
            }
            catch(Exception ex)
            {
                return false;
                throw ex;
            }
            return true;
        }
        internal Boolean Delete(int id)
        {
            try
            {
                var target = (from g in lqDB.Type where g.TypeId == id select g).ToList()[0];
                lqDB.Type.DeleteOnSubmit(target);
                lqDB.SubmitChanges();

            }
            catch(Exception ex)
            {
                return false;
                throw (ex);
            }
            return true;
        }
    }

}
